{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json, os, requests\n",
    "\n",
    "subscription_key = '74e44103f455499e95f90eb4a63ef3cd'\n",
    "assert subscription_key\n",
    "\n",
    "face_api_url = \"https://1119api.cognitiveservices.azure.com//face/v1.0/detect\"\n",
    "\n",
    "image_url = 'http://pic.qqtn.com/up/2018-6/15299823728306562.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "params = {\n",
    "    'detectionModel': 'detection_01',\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "    'returnFaceId': 'true'\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '0c178d46-1116-4db3-a857-fe68ad01a905',\n",
       "  'faceRectangle': {'top': 167, 'left': 148, 'width': 165, 'height': 165},\n",
       "  'faceAttributes': {'smile': 0.055,\n",
       "   'headPose': {'pitch': -15.7, 'roll': -4.0, 'yaw': 8.4},\n",
       "   'gender': 'female',\n",
       "   'age': 4.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.001,\n",
       "    'contempt': 0.004,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.055,\n",
       "    'neutral': 0.934,\n",
       "    'sadness': 0.003,\n",
       "    'surprise': 0.003},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.69},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.3},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': True},\n",
       "   'hair': {'bald': 0.03,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'blond', 'confidence': 0.92},\n",
       "     {'color': 'red', 'confidence': 0.87},\n",
       "     {'color': 'other', 'confidence': 0.78},\n",
       "     {'color': 'gray', 'confidence': 0.21},\n",
       "     {'color': 'brown', 'confidence': 0.19},\n",
       "     {'color': 'black', 'confidence': 0.06},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df_face = pd.json_normalize(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.smile</th>\n",
       "      <th>faceAttributes.headPose.pitch</th>\n",
       "      <th>faceAttributes.headPose.roll</th>\n",
       "      <th>faceAttributes.headPose.yaw</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>...</th>\n",
       "      <th>faceAttributes.noise.value</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "      <th>faceAttributes.accessories</th>\n",
       "      <th>faceAttributes.occlusion.foreheadOccluded</th>\n",
       "      <th>faceAttributes.occlusion.eyeOccluded</th>\n",
       "      <th>faceAttributes.occlusion.mouthOccluded</th>\n",
       "      <th>faceAttributes.hair.bald</th>\n",
       "      <th>faceAttributes.hair.invisible</th>\n",
       "      <th>faceAttributes.hair.hairColor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0c178d46-1116-4db3-a857-fe68ad01a905</td>\n",
       "      <td>167</td>\n",
       "      <td>148</td>\n",
       "      <td>165</td>\n",
       "      <td>165</td>\n",
       "      <td>0.055</td>\n",
       "      <td>-15.7</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>8.4</td>\n",
       "      <td>female</td>\n",
       "      <td>...</td>\n",
       "      <td>0.3</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>[]</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>0.03</td>\n",
       "      <td>False</td>\n",
       "      <td>[{'color': 'blond', 'confidence': 0.92}, {'col...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  0c178d46-1116-4db3-a857-fe68ad01a905                167   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 148                  165                   165   \n",
       "\n",
       "   faceAttributes.smile  faceAttributes.headPose.pitch  \\\n",
       "0                 0.055                          -15.7   \n",
       "\n",
       "   faceAttributes.headPose.roll  faceAttributes.headPose.yaw  \\\n",
       "0                          -4.0                          8.4   \n",
       "\n",
       "  faceAttributes.gender  ...  faceAttributes.noise.value  \\\n",
       "0                female  ...                         0.3   \n",
       "\n",
       "   faceAttributes.makeup.eyeMakeup  faceAttributes.makeup.lipMakeup  \\\n",
       "0                             True                            False   \n",
       "\n",
       "   faceAttributes.accessories faceAttributes.occlusion.foreheadOccluded  \\\n",
       "0                          []                                     False   \n",
       "\n",
       "   faceAttributes.occlusion.eyeOccluded  \\\n",
       "0                                 False   \n",
       "\n",
       "   faceAttributes.occlusion.mouthOccluded  faceAttributes.hair.bald  \\\n",
       "0                                    True                      0.03   \n",
       "\n",
       "   faceAttributes.hair.invisible  \\\n",
       "0                          False   \n",
       "\n",
       "                       faceAttributes.hair.hairColor  \n",
       "0  [{'color': 'blond', 'confidence': 0.92}, {'col...  \n",
       "\n",
       "[1 rows x 38 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "faceListId =  \"小娃子0\"\n",
    "create_facelists_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/xiaowazi0\"\n",
    "subscription_key = '74e44103f455499e95f90eb4a63ef3cd'\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "    \"name\": \"小孩相册\",\n",
    "    \"userData\": \"网图\",\n",
    "    \"recognitionModel\": \"recognition_02\",\n",
    "    \n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url,headers=headers,json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/xiaowazi0\"\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [],\n",
       " 'faceListId': 'xiaowazi0',\n",
       " 'name': '小孩相册',\n",
       " 'userData': '网图'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_face_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/xiaowazi/persistedfaces\"\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://pic.qqtn.com/up/2018-6/15299823728306562.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"小女孩\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': 'e608f2d4-b450-4104-983a-d4379a0e4b3d',\n",
       "   'userData': '小女孩'},\n",
       "  {'persistedFaceId': '054d81e3-3a21-4eca-8b11-fb56c5cb2efe',\n",
       "   'userData': '小女孩'}],\n",
       " 'faceListId': 'xiaowazi',\n",
       " 'name': '小孩相册',\n",
       " 'userData': '网图'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/facelists/xiaowazi\"\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '3808780c-5ef0-4c7a-b8b7-c1f429c163ec',\n",
       "  'faceRectangle': {'top': 167, 'left': 148, 'width': 165, 'height': 165}}]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_api_url = 'https://1119api.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "image_url = 'http://pic.qqtn.com/up/2018-6/15299823728306562.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    'recognitionModel':'recognition_03',\n",
    "    'detectionModel':'detection_01',\n",
    "    'returnFaceAttributes': '',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://1119api.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"3808780c-5ef0-4c7a-b8b7-c1f429c163ec\",\n",
    "    \"faceListId\": \"xiaowazi\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': 'e608f2d4-b450-4104-983a-d4379a0e4b3d',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': '054d81e3-3a21-4eca-8b11-fb56c5cb2efe',\n",
       "  'confidence': 1.0}]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e608f2d4-b450-4104-983a-d4379a0e4b3d</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>054d81e3-3a21-4eca-8b11-fb56c5cb2efe</td>\n",
       "      <td>小女孩</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData\n",
       "0  e608f2d4-b450-4104-983a-d4379a0e4b3d      小女孩\n",
       "1  054d81e3-3a21-4eca-8b11-fb56c5cb2efe      小女孩"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "faceListId_df = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "faceListId_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e608f2d4-b450-4104-983a-d4379a0e4b3d</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>054d81e3-3a21-4eca-8b11-fb56c5cb2efe</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  e608f2d4-b450-4104-983a-d4379a0e4b3d         1.0\n",
       "1  054d81e3-3a21-4eca-8b11-fb56c5cb2efe         1.0"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "find_df = pd.json_normalize(r_findsimilars.json())\n",
    "find_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e608f2d4-b450-4104-983a-d4379a0e4b3d</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>054d81e3-3a21-4eca-8b11-fb56c5cb2efe</td>\n",
       "      <td>小女孩</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "0  e608f2d4-b450-4104-983a-d4379a0e4b3d      小女孩         1.0\n",
       "1  054d81e3-3a21-4eca-8b11-fb56c5cb2efe      小女孩         1.0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(faceListId_df, find_df,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
